package com.tim.app.ui.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListPopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.application.library.util.TimeUtil;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.server.result.EveryDayHearListResult;

/**
 * 专辑详情的header
 */

public class MusicAlbumDetialHeadView extends RelativeLayout {

    public static final String TAG = "MusicAlbumDetialHeadView";

    private TextView tvStageCount;
    private TextView tvOrderBy;

    public MusicAlbumDetialHeadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvStageCount = (TextView) findViewById(R.id.tvStageCount);
        tvOrderBy = (TextView) findViewById(R.id.tvOrderBy);
        tvOrderBy.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showListPopup(tvOrderBy);
            }
        });
    }

    ListPopupWindow listPopupWindow;

    public void showListPopup(final TextView textView) {
        if (null == listPopupWindow) {
            final String items[] = {"最新排序", "最热排序"};
            listPopupWindow = new ListPopupWindow(getContext());

            ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getContext(), R.layout.pop_order_list_item, items);
            //设置ListView类型的适配器
            listPopupWindow.setAdapter(arrayAdapter);

            //给每个item设置监听事件
            listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (position == 0) {
                        textView.setText("最新");
                        EventManager.ins().sendEvent(EventTag.ORDER_BY_ALBUM_TIME, 0, 0, null);
                    } else {
                        textView.setText("最热");
                        EventManager.ins().sendEvent(EventTag.ORDER_BT_ALBUM_HOT, 0, 0, null);
                    }
                    listPopupWindow.dismiss();
                }
            });

            //设置ListPopupWindow的锚点,也就是弹出框的位置是相对当前参数View的位置来显示，
            listPopupWindow.setAnchorView(textView);

            //ListPopupWindow 距锚点的距离，也就是相对锚点View的位置
            listPopupWindow.setHorizontalOffset(0);
            listPopupWindow.setVerticalOffset(-textView.getHeight());

            //设置对话框的宽高
            listPopupWindow.setWidth(300);
            listPopupWindow.setHeight(300);
            listPopupWindow.setModal(false);

        }
        listPopupWindow.show();
    }


    /**
     * 刷新页面
     */
    public void setData(final int totalStageCount) {
        tvStageCount.setText(getContext().getString(R.string.stageCount, String.valueOf(totalStageCount)));
    }

}
